<?php
class User
{





    /**
     * Enthaelt alle Informationen ueber den Benutzer
     *
     * @var Array
     */
    private $userinfo;
    /**
     * Haelt die Benutzerrechte als Zahl in einem Array bereit
     *
     * @var Array
     */
    private static $userpermission = array(
	                                       'USER_CAN_LOGIN'                 =>    1,
	                                       'USER_CAN_ADD_DOWNLOAD'          =>    2,
	                                       'USER_CAN_EDIT_DOWNLOAD'         =>    4,
	                                       'USER_CAN_DELETE_DOWNLOAD'       =>    8,
	                                       'USER_CAN_ADD_BUDDYLIST'         =>    16,
	                                       'USER_CAN_DELETE_BUDDYLIST'      =>    32,
	                                       'USER_CAN_ADD_IGNORELIST'        =>    64,
	                                       'USER_CAN_DELETE_IGNORELIST'     =>    128,
	                                       'USER_CAN_IGNORE_ADMIN'          =>    256,
	                                       'USER_CAN_ADD_COMMENT'           =>    512,
	                                       'USER_CAN_EDIT_COMMENT'          =>    1024,
	                                       'USER_CAN_DELETE_COMMENT'        =>    2048,
                                           'USER_CAN_ADD_NEWSCOMMENT'	    =>    4096,
                                           'USER_CAN_EDIT_NEWSCOMMEN'	    =>    8192,
                                           'USER_CAN_DELETE_NEWSCOMMENT'    =>    16384
    );
    /**
     * Haelt die Adminrechte als Zahl in einem Array bereit
     *
     * @var Array
     */
    private static $adminpermission = array(
	                                        'ADMIN_CAN_EDIT_DOWNLOAD'       =>  1,
	                                        'ADMIN_CAN_DELETE_DOWNLOAD'     =>  2,
	                                        'ADMIN_CAN_MOVE_DOWNLOAD'       =>  4,
	                                        'ADMIN_CAN_ADD_LINKING'         =>  8,
	                                        'ADMIN_CAN_DELETE_LINKING'      =>  16,
	                                        'ADMIN_CAN_EDIT_NEWSCOMMENT'    =>  32,
	                                        'ADMIN_CAN_DELETE_NEWSCOMMENT'  =>  64
    );




    /**
     * Konstruktor
     * Laedt Benutzerinformationen des Benutzers mitID $userid aus und speichert sie in $this->userinfo
     *
     * @param Integer $userid
     */
    public function __construct($userid)
    {
        global $core;

        $query = 'SELECT
                	user.*,
                	usergroup.usergroupid AS usergroupid,
	                usergroup.name AS usergroupname,
	                usergroup.userpermission AS userpermission,
	                usergroup.adminpermission AS adminpermission,
	                usergroup.acppermission AS acppermission
	              FROM `'.TABLEPREFIX.'user` AS user
	              LEFT JOIN `'.TABLEPREFIX.'usergroup` ON (user.usergroupid = user.usergroupid)
	              WHERE user.userid=:userid
	              LIMIT 1';
        $core->db->prepare($query);
        $core->db->bind('userid',$userid,Database::PARAM_INT);
        $core->db->execute();
        $array = $core->db->fetchAll();
        $core->db->free();
        if (!$array)
        {
            return FALSE;
        }

        foreach ($array[0] as $key => $value)
        {
            $this->userinfo[$key] = $value;
        }
        unset($query,$array,$userid,$value,$key);
    }




    /**
     * Speichert die Berechtigungen eine Benutzers als Boolean-Werte in einem Array
     *
     * @param Integer $userpermission
     * @param Integer $adminpermission
     * @param Integer $acppermission
     */
    private function permission2string($userpermission,$adminpermission,$acppermission)
    {
        foreach (self::$userpermission as $key => $value)
        {
            $this->userinfo['userpermission'][$key] = (bool)($value & $userpermission);
        }

        foreach (self::$adminpermission as $key => $value)
        {
            $this->userinfo['adminpermission'][$key] = (bool)($value & $adminpermission);
        }

        foreach (self::$acppermission as $key => $value)
        {
            $this->userinfo['acppermission'][$key] = (bool)($value & $acppermission);
        }
        unset($userpermission,$acppermission,$adminpermission);
    }







    /**
     * Liefert eine Information ueber den Benutzer
     *
     * @param String $name
     * @return Mixed
     */
    public function get($name,$sub1 = NULL)
    {
        if ($sub1)
        {
            return $this->userinfo[$name][$sub1];
        }
        return($this->userinfo[$name]);
    }




    /**
     * Destruktor
     */
    public function __destruct()
    {
        unset($this->userinfo);
    }




}
?>